﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShiXuLong.Service;
using ShiXuLong.Model;

namespace ShiXuLong.Admin.Controllers
{
   
    public class ShopController : Controller
    {
        ShopManagement sp = new ShopManagement();
        // GET: Shop
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult CheckView(int id)
        {
            ViewBag.ShopType = sp.GetShopType();
            var list = sp.GetShopById(id);
            return View(list[0]);
        }

        public ActionResult photos(int Id)
        {
            ViewBag.Id = Id;
            return View();
        }
        /// <summary>
        /// 图片管理页面数据接口
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ActionResult GetImg(int limit, int page, int Id)
        {
            int count = 0;
            var lists = sp.GetImgData(limit, page, out count, Id);
            return Json(new { code = 0, msg = "", count = count, data = lists }, JsonRequestBehavior.AllowGet);

        }
        /// <summary>
        /// 图片上传功能
        /// </summary>
        /// <returns></returns>
        public ActionResult UploadPhoto()
        {
            try
            {
                //上传的单独文件的访问
                HttpPostedFileBase postFile = Request.Files["file1"];
                if (postFile != null)
                {
                    //文件扩展名，不含“.”
                    string fileExt = postFile.FileName.Substring(postFile.FileName.LastIndexOf(".") + 1);
                    //随机生成新的文件名
                    string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + "." + fileExt;
                    //上传目录相对路径~表示网站根目录
                    string upLoadPath = "~/Content/";
                    //上传目录的物理路径
                    string fullUpLoadPath = Server.MapPath(upLoadPath);
                    //核心方法，上传文件并保存
                    postFile.SaveAs(fullUpLoadPath + newFileName);
                    return Json(new { success = true, message = "", remark = "Content/" + newFileName });
                }
                else
                {
                    return Json(new { success = false, message = "未检测到文件数据" });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "上传失败:" + ex.Message });
            }
        }


        /// <summary>
        /// 添加图片视图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ImgTianJia(int ids)
        {
            var lists = sp.GetProId(ids);
            if (lists.Count != 0)
            {
                ViewBag.pro = lists[0].ShopId;
            }
            else
            {
                ViewBag.pro = ids;
            }

            return View();
        }
        /// <summary>
        /// 添加方法
        /// </summary>
        /// <param name="protype"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ImgTianJia(Photo picimg)
        {
            var b = sp.ImgAdd(picimg);
            if (b > 0)
            {
                return Json(new { success = true, msg = "添加成功！" });
            }
            else
            {
                return Json(new { success = false, msg = "添加失败！" });
            }
        }
        // <summary>
        /// 编辑方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditImg(Photo pro)
        {
            var n = sp.XiuGaiImg(pro);
            if (n > 0)
            {
                return Json(new { success = true, msg = "修改成功！" });
            }
            else
            {
                return Json(new { success = false, msg = "修改失败！" });
            }
        }
        /// <summary>
        /// 图片删除方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelDataImg(int id)
        {
            return sp.DelProImg(id);
        }

        public ActionResult ShopTypeView()
        {
            return View();
        }
        public ActionResult GetShopType(int page, int limit, string spuName) {

            int count = 0;
            var lists = sp.ProductTypeSyes(page, limit, spuName, out count);
            return Json(new { code = 0, msg = "", count = count, data = lists }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult ProductTypeAdd()
        {
            return View();
        }
        /// <summary>
        /// 调用添加方法
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ProductTypeAdd(ShopType re)
        {
            var num = sp.ServiceTypeAdd(re);
            if (num > 0)
            {
                return Json(new { success = true, msg = "添加成功" });
            }
            return Json(new { success = true, msg = "添加失败" });

        }
        [HttpGet]
        public ActionResult TypeEditView(int id)
        {
            var spu = sp.GetInfo(id);
            return View(spu);
        }
        /// <summary>
        /// 调用编辑方法
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult TypeEdit(ShopType re)
        {
            var num = sp.TypeEdit(re);
            if (num > 0)
            {
                return Json(new { success = true, msg = "修改成功" });
            }
            return Json(new { success = true, msg = "修改失败" });
        }
        /// <summary>
        /// 调用删除方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult DelProductType(int id)
        {
            int num = sp.DelProductType(id);
            return Content(num.ToString());
        }


    }

}